package com.lab.authservice.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//Access uri ---> swagger-ui.html
@Configuration
@EnableSwagger2
public class Swagger2Config {
	@Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
        		.select()
                // 方法需要有ApiOperation注解才能生存接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 路径使用any风格
                .paths(PathSelectors.any())
                .build()
                // 如何保护我们的Api，有三种验证（ApiKey, BasicAuth, OAuth）
                .securitySchemes(security())
                .securityContexts(securityContexts())
                // 接口文档的基本信息
                .apiInfo(apiInfo());
    }
    
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Book-API文档")
                .description("本文档描述了Book接口定义")
                .version("1.0")
                .contact(new Contact("Jazz", "http://www.666.com", "666@qq.com"))
                //.termsOfServiceUrl("http://localhost:8002")
                .build();
    }

    private List<? extends SecurityScheme> securitySchemes() {
        return Arrays.asList(new BasicAuth("Authorization"));
    }

    private List<ApiKey> security() {
        ArrayList<ApiKey> apiKeys = new ArrayList<ApiKey>();
        apiKeys.add(new ApiKey("abc", "123", "header"));
        return apiKeys;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences=new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
}
